import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: LoginView
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }, {
    path: '/login',
    name: 'login',
    component: LoginView
  }, {
    path: '/admin-index',
    name: 'admin-index',
    component: () => import("../views/AdminIndexView.vue"),
    children: [{
      path: '',
      name: 'Main',
      component: () => import("../views/IndexView.vue")
    }, {
      path: "adminlist",
      component: () => import("../views/admin/List.vue"),
    }, {
      path: "gcate",
      name: "GoodsCate",
      component: () => import("../views/goods/cate/List.vue"),
    }, {
      path: "fundlist",
      name: "PayOrder",
      component: () => import("../views/fund/PayOrderList.vue"),
    }, {
      path: "noticlist",
      name: "NoticList",
      component: () => import("../views/media/notice/List.vue"),
    }, {
      path: "bannerlist",
      name: "BannerList",
      component: () => import("../views/media/banner/List.vue"),
    }, {
      path: "orderlist",
      name: "OrderList",
      component: () => import("../views/order/List.vue"),
    }, {
      path: "goods",
      name: "GoodsList",
      component: () => import("../views/goods/goods/List.vue"),
    }
      , {
      path: "goodsadd",
      name: "GoodsAdd",
      component: () => import("../views/goods/goods/Add"),
    }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to) => {
  if (to.path == "/" || to.path == "/login") {
    // console.log("正常访问 没啥可干的");
    return true
  } else {
    let token = localStorage.token;// 获取缓存中token
    let token_outtime = localStorage.token_outtime // 获取缓存token超期时间
    let now = Date.now()  // 获取当前时间戳

    // 如果当前时间戳比缓存超期时间大 就强制跳转到登录页面
    // 或者 token没有内容也跳转到登录页面
    // console.log(now > token_outtime);
    if (now > token_outtime || !token) {
      // token 超期 跳转登录页面
      // console.log("a");
      router.push("/login")
    } else {
      // 验证token是否合法
      return true
    }
  }
})
export default router
